//
//  DWMemoryViewController.m
//  OCAPPDemo
//
//  Created by OS on 2022/3/2.
//

#import "DWMemoryViewController.h"
#import "DWMemoryStudent.h"
#import <objc/runtime.h>
#import <malloc/malloc.h>

@interface DWMemoryViewController ()

@end

@implementation DWMemoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}


- (IBAction)classAlign:(id)sender {
    
    DWMemoryPerson *p = [[DWMemoryPerson alloc] init];
    DWMemoryStudent *s = [[DWMemoryStudent alloc] init];
    
    size_t sp = class_getInstanceSize([p class]);
    size_t mp = malloc_size((__bridge const void *)p);
    
    size_t ss = class_getInstanceSize([s class]);
    size_t ms = malloc_size((__bridge const void *)s);
    
    NSLog(@"\n %zd %zd \n  %zd %zd", sp, mp, ss, ms);
}


- (IBAction)align:(id)sender {
    
    struct A { // 24
        char a;   // 0 - 1
        double b; // 8 - 8
        int c; // 12 - 4
        short d; // 16 - 2
    };
    
    struct B { // 16
        double b;
        int c;
        short d;
        char a;
    };
    
    struct C { // 56
        struct A a; // 0 - 24
        int b; // 24 - 4
        struct B c; // 32 - 16
        double d; // 48 - 8
    };
    
    
    struct A a;
    a.a = 'a';
    a.b = 4.0;
    a.c = 2;
    a.d = 2;
    struct B b;
    b.a = 'b';
    b.b = 5.0;
    b.c = 5;
    b.d = 5;
    struct C c;
    c.a = a;
    c.b = 8;
    c.c = b;
    c.d = 10.2;
    NSLog(@"A = %zd, B = %zd, C = %zd", sizeof(a), sizeof(b), sizeof(c));
    NSLog(@"%p, %p", &c, &c.c);
}

@end
